package cmu.radiosense;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

//import java.net.HttpURLConnection;
//import java.net.URL;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import cmu.android.RadioSense.R;
import cmu.android.RadioSense.R.id;
import cmu.android.RadioSense.R.layout;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class RadioSense extends Activity {
    /** Called when the activity is first created. */
    
    private static final int REQUEST_ENABLE_BT = 3;
    
    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data)
    {
    	
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TextView tv = (TextView) findViewById(R.id.textView1);
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            tv.setText("Sorry your device does not support bluetooth");
            return;
        }
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";  
        
        String[] projection = { MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.DATA};
          
        Cursor cursor = this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection, selection, null, null);
        StringBuilder songs = new StringBuilder("Songs: \n");
        while (cursor.moveToNext())
        {
        	songs.append(cursor.getString(0) + " - " + cursor.getString(1) + " - " + cursor.getString(2)+ "\n");
        }
        setContentView(R.layout.main);
        tv.setText(songs);/*
        final EditText et = (EditText) findViewById(R.id.editText1);
        final Button button = (Button) findViewById(R.id.button1);
        final RadioButton pop = (RadioButton) findViewById(R.id.pop);
        final RadioButton rock = (RadioButton) findViewById(R.id.rock);
        final RadioButton country = (RadioButton) findViewById(R.id.country);
        final RadioButton jazz = (RadioButton) findViewById(R.id.jazz);
        final RadioButton randb = (RadioButton) findViewById(R.id.randb);
        final RadioButton sports = (RadioButton) findViewById(R.id.sports);
        final RadioButton religious = (RadioButton) findViewById(R.id.religious);
        final RadioButton news = (RadioButton) findViewById(R.id.news);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
                try {
                	ArrayList<String> genres = new ArrayList<String>();
                	StringBuilder g = new StringBuilder("");
                	if(pop.isChecked())
                		genres.add("pop");
                	if(rock.isChecked())
                		genres.add("rock");
                	if(country.isChecked())
                		genres.add("country");
                	if(jazz.isChecked())
                		genres.add("jazz");
                	if(randb.isChecked())
                		genres.add("randb");
                	if(sports.isChecked())
                		genres.add("sports");
                	if(religious.isChecked())
                		genres.add("religious");
                	if(news.isChecked())
                		genres.add("news");
                	if(genres.isEmpty())
                		g.append("none");
                	else
                		g.append(genres.get(0));
                	for(int i=1; i<genres.size(); i++)
                		g.append(","+genres.get(i));
                	final String ipaddress = et.getText().toString();
                	HttpClient httpClient = new DefaultHttpClient();
                	HttpGet request = new HttpGet("http://" + ipaddress +":8080/RadioSenseServer/RS.do?a=Coldplay&t=Trouble&g="+g);//a=Coldplay,Duck%20Sauce,Lady%20Gaga,Eminem,Bag%20Raiders,Gorillaz,Led%20Zeppelin,Mumford%20and%20Sons&t=Trouble,Barbra%20Streisand,Paparazzi,Superman,Shooting%20Stars,Dare,Kashmir,The%20Cave");//Form GET request
                	HttpResponse response = httpClient.execute(request); //connect to server
                	int status = response.getStatusLine().getStatusCode();
                	if (status != HttpStatus.SC_OK) {
                		tv.setText("Could not connect to server");
                	ByteArrayOutputStream ostream = new ByteArrayOutputStream();
                	response.getEntity().writeTo(ostream);
                	
                	} else {
                		InputStream content = response.getEntity().getContent();
                		StringBuilder sb = new StringBuilder("");
                		byte x = 0;
                		while(((x = (byte)content.read()) >= 0))
                			sb.append((char)x);
                		tv.setText(sb);
                		if(pop.isChecked())
                    		pop.setChecked(false);
                    	if(rock.isChecked())
                    		rock.setChecked(false);
                    	if(country.isChecked())
                    		country.setChecked(false);
                    	if(jazz.isChecked())
                    		jazz.setChecked(false);
                    	if(randb.isChecked())
                    		randb.setChecked(false);
                    	if(sports.isChecked())
                    		sports.setChecked(false);
                    	if(religious.isChecked())
                    		religious.setChecked(false);
                    	if(news.isChecked())
                    		news.setChecked(false);
                		content.close();
                		// this will also close the connection
                	}
                	
           } catch (Exception e) {
        	   /*StackTraceElement [] stackTrace = e.getStackTrace();
        	   StringBuilder sb = new StringBuilder("");
        	   for(int i=0; i<stackTrace.length; i++)
                  sb.append(e.getStackTrace()[i].getMethodName()+":"+e.getStackTrace()[i].getFileName()+":"+e.getStackTrace()[i].getLineNumber()+"\n");
                tv.setText(sb);  
               */
        	  /* e.printStackTrace();
        	   tv.setText("Could not connect to server");
              }
          
            }
        });
        */
         
         
    }
    /*
    public void getAllSongs() {

        Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

        cursor = managedQuery(allsongsuri, STAR, selection, null, null);

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    song_name = cursor
                            .getString(cursor
                                    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    int song_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media._ID));

                    String fullpath = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.DATA));
                    fullsongpath.add(fullpath);

                    album_name = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                    int album_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                    artist_name = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    int artist_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));


                } while (cursor.moveToNext());

            }
            cursor.close();
            db.closeDatabase();
        }
    }*/
}